//
//  NWNewFeatureViewController.swift
//  微博01
//
//  Created by 牛威 on 15/12/9.
//  Copyright © 2015年 牛威. All rights reserved.
//

import UIKit
import SnapKit

private let WBNWNewfeatureViewCell = "WBNWNewfeatureViewCell"
private let WBNWNewfeatureImageCount = 4

class NWNewFeatureViewController: UICollectionViewController {

    
    init(){
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        //这里要切记 等到super.init方法执行结束之后 视图里面的属性才会被创建出来 所以要把collectionView的设置放在super.init之后要不然会报错
    
        super.init(collectionViewLayout: layout)
        
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    
    }

     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
    
    //隐藏状态栏

    override func prefersStatusBarHidden() -> Bool {
        
        return true
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NWNewfeatureViewCell.self, forCellWithReuseIdentifier: WBNWNewfeatureViewCell)

    }



    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return WBNWNewfeatureImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBNWNewfeatureViewCell, forIndexPath: indexPath) as! NWNewfeatureViewCell
        
        
        cell.index = indexPath.item
        
        return cell
    }
    
    //滑动结束的时候进行动画的播放
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let pageIndex = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        //判断是否是最后一页
        
        if pageIndex != WBNWNewfeatureImageCount - 1 {
        
          return
        
        }
        
        //到这一步肯定是最后一页 进行动画播放
        
      let cell =  collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: pageIndex, inSection: 0)) as! NWNewfeatureViewCell
        
        
        //在同一个文件中 private没有限制
        
        cell.showAnimationBtn()
        
        
        
    }

}


//MARK: - 自定义cell

//写在同一个文件中的类一般是不想对外开放的 



private class NWNewfeatureViewCell : UICollectionViewCell {
    
    
    //MARK: - 按钮的监听方法 进入未登录的主界面 一定要加上@Objc不然找不到方法
    
   @objc private func startBtnClick(){
    
    
         //发送通知
    
    NSNotificationCenter.defaultCenter().postNotificationName(WBChangeRootViewControllerNotification, object: nil)
        
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //这里的frame取决于layout中的设置的itemSize
        
//        print(frame)
        
        setupUI()
        
        
    }
    

    
   //MARK: - weiimageView添加image计算型属性
    
    private var index : Int = 0  {
        
        
    
        didSet{
            
            
        
            iconView.image = UIImage(named: "new_feature_\(index + 1)")

            //需要考虑动画播放的时机 在滑动结束的时候进行动画播放
            
//            showAnimationBtn()
            
            //在开始隐藏startBtn 坑1 不能在180位置设置隐藏btn,因为cell的重用会使cell向前滑的的时候再次出现btn所以要在此设置btn的隐藏 这样就会避免了cell的重用
            
            startBtn.hidden = true
        }
    
    }
    
    //MARK: - 显示动画按钮
    
    private func showAnimationBtn(){
        
       startBtn.hidden = false
        
        startBtn.userInteractionEnabled = false
    
      startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(2,         //持续时间
            delay: 0.25,                         //延迟时间
            usingSpringWithDamping: 0.5,      //阻尼  越小弹的越厉害
            initialSpringVelocity: 10,        //初始速度
            options: [],  //动画选项
            animations: { () -> Void in
                
                self.startBtn.transform = CGAffineTransformIdentity
                
            }) { (_) -> Void in
                
                self.startBtn.userInteractionEnabled = true
                
        }
    
    
    }
    
    
    private func setupUI(){
    
            //1 添加控件
        
        addSubview(iconView)
    
        addSubview(startBtn)
        
//        startBtn.hidden = true
        
            //2设置尺寸
        iconView.frame = self.bounds
        
        startBtn.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.8)
        }
        
        startBtn.addTarget(self, action: "startBtnClick", forControlEvents: .TouchUpInside)
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private let iconView = UIImageView()
    private let startBtn = UIButton(title: "开始体验", backImageName: "new_feature_finish_button", color: UIColor.darkGrayColor())
    


}
